/**
 * Created by TEANA on 2018/1/2.
 */

module.exports = ngModule => {
  ngModule.factory('Argument', () => {
    return {
      /*
       *
       * @TODO 将URL中的a=a&b=b参数转换为{a:a,b:b}对象
       * @params url 带转换的URL
       *
       * */
      getArgs(url) {
        let reg_url = /^[^\?]+\?([\w\W]+)$/,
          reg_para = /([^&=]+)=([\w\W]*?)(&|$|#)/g,
          arr_url = reg_url.exec(url),
          ret = {};
        if (arr_url && arr_url[1]) {
          let str_para = arr_url[1], result;
          while ((result = reg_para.exec(str_para)) != null) {
            ret[result[1]] = result[2];
          }
        }
        return ret;
      },
      /*
       *
       * @TODO 将URL中的a=a&b=b参数中的某个值
       * @param url 带转换的URL
       * @param key 关键字[key]
       * */
      getArg(url, key) {
        let args = this.getArgs(url);
        return eval('args.' + key);
      },
      /*
       *
       * @TODO 格式化参数[错误检查]
       * @param url 带转换的URL
       * */
      formatArgs(url) {
        let pattern = (this.getArg(url, 'pattern') !== '1' ? '0' : '1');
        let appId = (this.getArg(url, 'appId') !== 'xm' ? 'xf' : 'xm');
        let autoload = (this.getArg(url, 'autoload') !== '1' ? '0' : '1');
        let s = (this.getArg(url, 's') ? this.getArg(url, 's') : '');
        let nav = (this.getArg(url, 'nav') ? this.getArg(url, 'nav') : '');
        return {
          pattern: pattern,
          appId: appId,
          autoload: autoload,
          s: s,
          nav: nav
        };
      },
      formatURL(url) {
        let args = this.formatArgs(url);
        let str = '';
        for (let key in args) {
          if (!args[key] == '') {//判断为空就不传
            str += ('&' + key + '=' + args[key]);
          }
        }
        if (str.startsWith('&')) {
          str = str.substr(1);
        }
        return str;
      }
    };
  });

  ngModule.factory('String', () => {
    return {
      startWith(str, start) {
        let reg = new RegExp("^" + start);
        return reg.test(str);
      },
      endWith(str, end) {
        let reg = new RegExp(end + "$");
        return reg.test(str);
      }
    };
  });

  /**
   *  addClass:为指定的dom元素添加样式
   *  removeClass:删除指定dom元素的样式
   *  toggleClass:如果存在(不存在)，就删除(添加)一个样式
   *  hasClass:判断样式是否存在
   * */
  ngModule.factory('Dom', function () {
    return {
      hasClass: function (obj, cls) {
        return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
      },
      addClass: function (obj, cls) {
        if (!this.hasClass(obj, cls)) obj.className += " " + cls;
      },
      removeClass: function (obj, cls) {
        if (this.hasClass(obj, cls)) {
          var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
          obj.className = obj.className.replace(reg, ' ');
        }
      },
      toggleClass: function (obj, cls) {
        if (this.hasClass(obj, cls)) {
          this.removeClass(obj, cls);
        } else {
          this.addClass(obj, cls);
        }
      },
      nextNode: function (node) {
        if (!node.nextSibling) return null;
        var nextNode = node.nextSibling;
        if (nextNode.nodeType == 1) {
          return nextNode;
        }
        return this.nextNode(node.nextSibling);
      }
    };
  });


};
